package com.zhl.service.impl;

import com.zhl.dao.UserDao;
import com.zhl.entity.User;
import com.zhl.service.UserService;
import com.zhl.utils.TransactionManager;

import java.util.List;

/**
 * 业务实现层
 *
 *
 * 控制事务
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> findAllUser() {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            List<User> list = userDao.findAllUser();
            //提交事务
            transactionManager.commit();
            return list;
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }
    }

    @Override
    public User findOne(Integer id) {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            User user = userDao.findOne(id);
            //提交事务
            transactionManager.commit();
            return user;
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }
    }

    @Override
    public void addUser(User user) {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            userDao.addUser(user);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }

    }

    @Override
    public void deleteUser(Integer id) {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            userDao.deleteUser(id);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }

    }

    @Override
    public void updateUser(User user) {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            userDao.updateUser(user);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }

    }

    @Override
    public void transferUser(String sourceName, String targetName, Double money) {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            User sourceUser = userDao.findUserByName(sourceName);
            User targetUser = userDao.findUserByName(targetName);

            sourceUser.setMoney(sourceUser.getMoney()-money);
            targetUser.setMoney(targetUser.getMoney()+money);

//            int i= 1/0;
            userDao.updateUser(sourceUser);

            userDao.updateUser(targetUser);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }



    }

    @Override
    public void findUserByName(String userName) {
        try {
            //开启事务
            transactionManager.benginTransaction();
            //执行业务操作
            userDao.findUserByName(userName);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //事务回滚
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放资源
            transactionManager.close();
        }

    }
}
